<?php

namespace App\Http\Controllers\Admin\Authorization;

use App\B2BProject\Admin\AdminControllerBuildTrait;
use App\B2BProject\Admin\Authority\Rbac\Role;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminRoleController extends Controller
{
    use AdminControllerBuildTrait;

    protected $moduleName = '';

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if ($request->ajax()) {
            $collection = Role::orderBy('created_at', 'desc')->paginate($request->query('pageSize'));

            $data = [];

            foreach ($collection as $role) {
                $data[] = [
                    'id'        => $role->id,
                    'username'  => $role->display_name,
                    'editorurl' => route('admin.role.edit', [$role->id]),
                    'deleurl'   => route('admin.role.destroy', [$role->id]),
                ];
            }

            return response()->json([
                'data'           => $data,
                'pagingConfData' => [
                    'page'     => $collection->currentPage(),
                    'pageSize' => $request->query('pageSize'),
                    'total'    => $collection->total(),
                ],
            ]);
        }

        return view('admin.authorization.role.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.authorization.role.edit');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return \DB::transaction(function () use ($request) {
            $role = new Role();

            $role->name         = $request->input('mark_name');
            $role->display_name = $request->input('name');
            $role->description  = $request->input('desc');

            $role->save();

            $role->permissions()->sync($request->input('rights', []));

            return redirect()->route('admin.role.index');
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = Role::findOrFail($id);

        return view('admin.authorization.role.edit', ['role' => $role]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return \DB::transaction(function () use ($request, $id) {
            $role = Role::findOrFail($id);

            $role->name         = $request->input('mark_name');
            $role->display_name = $request->input('name');
            $role->description  = $request->input('desc');
            $role->permissions()->sync($request->input('rights', []));

            $role->save();

            return redirect()->route('admin.role.index');
        });

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        $role = Role::findOrFail($id);

        $role->delete();

        return $this->index($request);
    }


    public function batchRemove(Request $request)
    {
        return \DB::transaction(function () use ($request) {
            $ids = $request->input('ids', []);

            foreach ($ids as $id) {
                Role::findOrFail($id)->delete();
            }

            return $this->index($request);
        });
    }
}
